package com.pdh.controller;

import com.pdh.bean.Courier;
import com.pdh.bean.Message;
import com.pdh.mvc.ResponseBody;
import com.pdh.service.CourierService;
import com.pdh.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class CourierController {
    @ResponseBody("/courier/consoleCourier.do")
    public String consoleUser(HttpServletRequest req, HttpServletResponse resp) {
        //有一个固定的消息对象
        Message message = CourierService.consoleCourier();
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        //1.接收参数
        boolean paging = Boolean.parseBoolean(req.getParameter("paging"));
        int offset = Integer.parseInt(req.getParameter("offset"));
        int limit = Integer.parseInt(req.getParameter("limit"));
        //2.进行分页查询 得不到数据
        Message msg = CourierService.findAll(paging, offset, limit);
        //返回的json必须是ResultData格式的
        String json = JSONUtil.toJSON(msg.getData());
        return json;
    }


    @ResponseBody("/courier/findByCourierPhone.do")
    public String findByCourierPhone(HttpServletRequest req, HttpServletResponse resp) {
        //1.接收参数
        String courierPhone = req.getParameter("courierPhone");
        //2.调用方法查询对应的用户
        Message message = CourierService.findByCourierPhone(courierPhone);
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) {
        //1.接收四个参数
        String courierName = req.getParameter("courierName");
        String courierPhone = req.getParameter("courierPhone");
        String courierIdCardNo = req.getParameter("courierIdCardNo");
        String password = req.getParameter("password");
        Courier courier = new Courier(courierName, courierPhone, password, courierIdCardNo);
        //2.操作
        Message message = CourierService.insert(courier);
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        //1.接收参数
        int id = Integer.parseInt(req.getParameter("id"));
        String courierName = req.getParameter("courierName");
        String courierPhone = req.getParameter("courierPhone");
        String password = req.getParameter("password");
        String courierIdCardNo = req.getParameter("courierIdCardNo");
        int status = Integer.parseInt(req.getParameter("status"));
        //2.创建新User对象
        Courier newCourier = new Courier(courierName, courierPhone, password, courierIdCardNo,status);
        //3.执行更新操作
        Message message = CourierService.update(id, newCourier);
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        //1.获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        //2.执行删除操作
        Message message = CourierService.delete(id);
        String json = JSONUtil.toJSON(message);
        return json;
    }
}
